import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzModalService } from 'ng-zorro-antd';
import { RechargeService } from 'src/app/service/recharge.service';

@Component({
  selector: 'app-online-pay',
  templateUrl: './online-pay.component.html',
  styleUrls: ['./online-pay.component.less']
})
export class OnlinePayComponent implements OnInit {

  validateForm!: FormGroup;
  payImages: string | null;
  loading: boolean = false;
  
  changeAmount(value: any) {
    console.log(value);
    let data = this.validateForm.value;
    data['amount'] = value;
    console.log(data);
    this.validateForm.setValue({ 'amount': value });
  }

  submitForm() {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.status == "VALID") {
      var data = this.validateForm.value;
      data['type'] = '在线充值';
      this.rechargeService.save(data).subscribe(result => {
        this.validateForm.reset();
        for (const key in this.validateForm.controls) {
          this.validateForm.controls[key].markAsPristine();
          this.validateForm.controls[key].updateValueAndValidity();
        }
        this.modalService.success({
          nzTitle: '信息提示',
          nzContent: '对接支付后将会跳转支付页面完成支付!'
        });
      });
    }
  }

  constructor(
    private fb: FormBuilder,
    private modalService: NzModalService,
    private rechargeService: RechargeService
  ) { }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      amount: [null, [Validators.required, Validators.pattern(/^(0|[1-9]\d*)(\.\d{1,2})?$/)]],
      payStyle: ['微信', [Validators.required]]
    });
  }

}
